package com.itheima;

import com.itheima.mapper.SingerMapper;
import com.itheima.pojo.Singer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo {
    private static SqlSession sqlSession;
    private static SingerMapper mapper;
    @Before
    public void begin() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        mapper = sqlSession.getMapper(SingerMapper.class);
    }
    @After
    public void end(){
        sqlSession.commit();
        sqlSession.close();
    }
    /*
    1.查询所有
     */
    @Test
    public void selectAll(){
        List<Singer> singers = mapper.selectAll();
        for (Singer singer : singers) {
            System.out.println(singer);
        }
    }
    /*
    2.查询id
     */
    @Test
    public void selectById(){
        Singer singer = mapper.selectById(1);
        System.out.println(singer);
    }
    /*
    3.1多条件动态查询
     */
    @Test
    public void selectByConditions(){
        int status = 1;
        String singerName = "周";
        String companyName = "杰";

        Singer singer = new Singer();
        singer.setSingerName(singerName);
        singer.setCompanyName(companyName);
        singer.setStatus(status);

        List<Singer> singers = mapper.selectByConditions(singer);
        System.out.println(singers);
    }
    /*
    3.2多条件动态查询
    */
    @Test
    public void selectByCondition(){
        int status = 1;
        String singerName = "周";
        String companyName = "杰";
        String song = "江";

        Singer singer = new Singer();
//        singer.setSingerName(singerName);
//        singer.setCompanyName(companyName);
//        singer.setStatus(status);
        singer.setSong(song);

        List<Singer> singers = mapper.selectByCondition(singer);
        System.out.println(singers);
    }
    /*
    4.添加
     */
    @Test
    public void add(){
        int status = 1;
        String singerName = "周杰伦";
        String companyName = "杰威尔音乐有限公司";
        String song = "彩虹";
        int ordered = 100;

        Singer singer = new Singer();
        singer.setSingerName(singerName);
        singer.setCompanyName(companyName);
        singer.setStatus(status);
        singer.setSong(song);
        singer.setOrdered(ordered);

        int add = mapper.add(singer);
        System.out.println(add);
    }
    /*
    5.修改
     */
    @Test
    public void update(){
        int status = 1;
        int id = 4;
        String singerName = "周杰伦";
        String companyName = "杰威尔音乐有限公司";
        String song = "七里香";
        int ordered = 100;

        Singer singer = new Singer();
//        singer.setSingerName(singerName);
//        singer.setCompanyName(companyName);
//        singer.setStatus(status);
        singer.setSong(song);
        singer.setId(id);
//        singer.setOrdered(ordered);

        mapper.update(singer);

    }
    /*
    6.删除
     */
    //6.1 根据ID删除
    @Test
    public void deleteById(){
        mapper.deleteById(4);
    }
    //6.3 批量删除
    @Test
    public void deleteByIds(){
        mapper.deleteByIds(new int[]{1,2,5,6});
    }
}
